﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DrCarDrive.DBService;
using DrCarDrive.Wpf.WpfWindow;
using DrCarDrive.Util;
using System.Text.RegularExpressions;

namespace DrCarDrive.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for StartDrivePage.xaml
    /// </summary>
    public partial class StartDrivePage : Page
    {
        private static StartDrivePage instance;
        private DriveService driveService;
        
        public StartDrivePage()
        {
            InitializeComponent();
            driveService = new DriveService();
        }

        public static StartDrivePage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new StartDrivePage();
                }

      

                return instance;
            }
        }

        private void btnStartDrive_Click(object sender, RoutedEventArgs e)
        {
            if (validate() == true)
            {
                tbl_drive drive = new tbl_drive();
                drive.ID = GUIDGenerator.driveKey();
                drive.DRIVER_NAME = txtDriverName.Text;
                DateTime startDateTime = DateTime.ParseExact(dtpStartDateTime.Text, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
                drive.START_DATE = startDateTime;
                drive.START_ODOMETER = Double.Parse(txtStartOdometer.Text);
                drive.FROM = txtDriveFrom.Text;
                drive.TO = txtDriveTo.Text;
                drive.STATUS = true;
                drive.FKY_CAR = SessionData.car.ID;
                
                driveService = new DriveService();
                //MessageBox.Show();
                int status = driveService.save(drive);
                if (status > 0)
                {
                    SessionData.drive = drive;
                    MainWindow.Instance.ContentFrame.Content = DashboardPage.Instance;
                }
            }
            else
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                errorWindow.ShowDialog();
            }


            
        }

        private Boolean validate()
        {
            if (txtDriverName.Text == "" || txtStartOdometer.Text == "" || dtpStartDateTime.Value == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static void destroy()
        {
            instance = null;
        }

        private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex(@"[^0-9]{1,4}");
            //Regex regex = new Regex(@"[0-9]{1,4}(\.[0-9]{1,2})?");
            e.Handled = regex.IsMatch(e.Text);
        }

        private void txtStartOdometer_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = !IsNumberKey(e.Key) && !IsActionKey(e.Key);
        }
        private bool IsNumberKey(Key inKey)
        {
            if (inKey < Key.D0 || inKey > Key.D9)
            {
                if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
                {
                    return false;
                }
            }
            return true;
        }

        private bool IsActionKey(Key inKey)
        {
            return inKey == Key.Delete || inKey == Key.Back || inKey == Key.Tab || inKey == Key.Return || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt);
        }

        private void btnStartDriveCancel_Click(object sender, RoutedEventArgs e)
        {
            txtDriverName.Text = "";
            txtDriveFrom.Text = "";
            txtStartOdometer.Text = "";
            txtDriveTo.Text = "";
            dtpStartDateTime.Text = "";
        }

        private void txtDriveTo_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
		
    }
}
